This document give sample applets that can be use for graphing. Each one inherence the class “Coordinates”, which inherence the class Canvas. For each example, I will list all required java/class files, display a sample applet, and notes about how to use the source code provided.
Listing of all files
Coordinates, the superclass of:
Applet 1: CoordinateGraph.java
Requires:
This is the typical curve function graph. Here is the code sample from SampleCoordinateGraph.java:
CoordinateGraph graph; graph = new CoordinateGraph (getWidth()-10,getHeight()-10,Color.white); graph.newGraph (-2,6,-4,4,5,5); //x range = (-2*5 to 6*5) = (-10 to 30). add(graph);
The second line is the constructor, which sets the width, height, and background color. The next line is newGraph(double xMin, double xMax, double yMin, double yMax, double xFactor, double yFactor), which is from Coordinates.java. This line is optional, and if you don’t use it, Coordinates.java creates a -10 to 10 range graph. Use the first four parameters to set the graph’s range. xFactor and yFactor sets how much one grid line is. Passing a “1” will make it go 1,2,3,... etc. Passing it a “2” however makes is go 2,4,6,... etc. Of course, not passing the correct parameters will result in the graph not displaying correctly.
The other thing you must do is within the CoordinateGraph class. All you need to do is rewrite the procedure “protected double function(double x)” to whatever function you want to graph. Here is the above sample code:
protected double function(double x)
{//change this to display different functions
return x/(x-10);
}
Applet 2: TimeGraph.java
Requires:
Very similar to CoordinateGraph, with the only difference is that x and y are functions of time. The applet class is coded the same:
TimeGraph graph; graph = new TimeGraph (getWidth()-10,getHeight()-10,Color.white); graph.newGraph(-1,5,-1,10,5,10); add(graph);
... while the TimeGraph class has two procedures to be rewritten, one of x and another for y, each time you want to display a new graph:
protected double functionX(double x)
{//change this to display different functions
return 2*x;
}
protected double functionY(double y)
{//change this to display different functions
return y*y;
}
Applet 3: PolarGraph.java
Requires:
The difference this time from CoordinateGraph is that this displays r as functions of theta, resulting in polar graphs. The applet class is the same as always:
PolarGraph graph; graph = new PolarGraph(getWidth()-10,getHeight()-10,Color.white); graph.newGraph(-2, 2, -1, 3, .5, .5); add(graph);
and the function method within PolarGraph now returns r when passed theta:
protected double function(double theta)
{//change this to display different functions
return Math.sin(theta*Math.PI/180);
}
Applet 4: PieChart.java
Requires:
In addition, you will want to be sure to include some form of key.
Different, but not complicated. PieChart does all the work with PolarGraph, all you need to do is input the data to PieChart by calling addData(double data, Color color). Here is the code from the sample applet:
public void init()
{
PieChart pieChart;
pieChart = new PieChart (getWidth()-10,getHeight()-10,Color.white);
addData(pieChart);
add(pieChart);
}
private void addData(PieChart pieChart)
{
pieChart.addData(1,Color.red);
pieChart.addData(1.5,Color.yellow);
pieChart.addData(2.5,Color.gray);
pieChart.addData(4,Color.green);
pieChart.addData(6,Color.blue);
}
Applet 5: BarChart.java
Requires:
In addition, you will want to be sure to include some form of key.
Very similar to PieChart in use. BarChart does all the work, all you need to do is call addData(double data, Color color). Here is the code from the sample applet:
public void init()
{
BarGraph barGraph;
barGraph = new BarGraph (getWidth()-10,getHeight()-10,Color.white);
addData(barGraph);
add(barGraph);
}
private void addData(BarGraph barGraph)
{
barGraph.addData(1,Color.red);
barGraph.addData(1.5,Color.yellow);
barGraph.addData(2.5,Color.gray);
barGraph.addData(4,Color.green);
barGraph.addData(6,Color.blue);
}
Brian Ogan
sabre@famvid.com